var ns;
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        showchoose: false,
        showsearch: false,//遮罩层
        chooseList: [],
        ID: 0,//工单ID
        IsAllChecked: false,//全选
        AllCategoryID: 0,//顶部选择的仓库
        AllCategoryName: "",
        IsCKOpen: false,
        CKList: [],
        CKSelectType: 0,
        CKSelectIndex: 0,
    },
    methods: {
        //选择仓库
        do_chooseCK: function (type, index) {
            var that = this;
            that.CKSelectType = type;
            that.CKSelectIndex = index;
            that.IsCKOpen = true;
        },
        doCloseCK: function () {
            var that = this;
            that.IsCKOpen = false;
        },
        doSelectCK: function (itemc) {
            var that = this;
            that.IsCKOpen = false;
            //赋值
            if (that.CKSelectType == 1) {
                that.list.forEach(item => {
                    item.CKCategoryID = itemc.id;
                    item.CKCategoryName = itemc.name;
                    // if(item.ischecked){
                    //     item.CKCategoryID=itemc.id;
                    //     item.CKCategoryName=itemc.name;
                    // }
                });
            }
            else {
                that.list[that.CKSelectIndex].CKCategoryID = itemc.id;
                that.list[that.CKSelectIndex].CKCategoryName = itemc.name;
            }
        },
        onClickChooseAll() {
            var that = this;
            var newlist = [];
            that.list.forEach(item => {
                item.CKCategoryID = 0;
                item.ischecked = that.IsAllChecked;
                newlist.push(item);
            });
            if (that.IsAllChecked) {
                that.chooseList = newlist;
            }
            else {
                that.chooseList = [];
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影
        },
        onClickChooseItem(item, type) {
            var that = this;
            item.ischecked = !item.ischecked;
            if (item.ischecked) {
                const index = that.chooseList.findIndex(p => p.ID === item.ID);
                if (index > -1) {
                    that.chooseList.splice(index, 1)
                }
                that.chooseList.push(item);
                const new_list = [...new Set(that.chooseList)];// 去重
                that.chooseList = new_list;
            }
            else {
                const index = that.chooseList.findIndex(p => p.ID === item.ID);
                if (index > -1) {
                    that.chooseList.splice(index, 1)
                }
            }
        },
        onClickChooseDel(item) {
            var that = this;
            const indexlist = that.list.findIndex(p => p.ID === item.ID);
            if (indexlist > -1) {
                that.list[indexlist].ischecked = false;
            }
            const index = that.chooseList.findIndex(p => p.ID === item.ID);
            if (index > -1) {
                that.chooseList.splice(index, 1)
            }
        },
        get_data: function () {
            var that = this;
            that.CKList = [];
            var options = {
                action: 'getuserrolecklist',
            };
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.CKList = data.data;
                } else if (err) {
                    that.CKList = [];
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_search: function () {
            var that = this;
            that.get_data();
        },
        do_choose: function (item) {
            var that = this;
            item.ischecked = !item.ischecked;
        },
        // 增加物料领用时  判断全局变量是申请单还是出库单
        do_save: function () {
            var that = this;
            var list = [];
            list = that.chooseList;
            if (list.length == 0) {
                api.toast({
                    msg: '请选择数据',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            //仓库不能为空
            var invalidRowError = '';
            list.forEach((row,index)=>{
                if (row.CKCategoryID == '' || row.CKCategoryID ==0||row.CKCategoryID ==undefined) {
                    invalidRowError += (index + 1) + ',';
                    return false;
                }
            });
            if (invalidRowError != '') {
                api.toast({
                    msg: "请选择仓库",
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options = {};
            options.ID = that.ID;
            options.productList = JSON.stringify(list);
            options.action = "saveproductapplication";
            options.IsServiceOut=true;
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(succeed));
                // console.log(JSON.stringify(data));
                // console.log(JSON.stringify(err));
                if (data.status) {
                    api.toast({
                        msg: "操作成功",
                        duration: 2000,
                        location: 'bottom'
                    });
                    //刷新详情界面
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 1000);
                } else if (!data.status) {
                    api.toast({
                        msg: data.Msg == undefined ? '提交失败' : data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
            });
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.list = api.pageParam.list || [];
    app.ID = api.pageParam.ID || 0;
    app.get_data();//获取仓库列表
    
    //默认选中全部
    app.IsAllChecked=true;
    app.onClickChooseAll();

    //关闭物品申请页面
    api.closeWin({
        name:'chooseckproduct_frm'
    });
}
